//
//  PMineResumeIntentionViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/25.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PMineResumeIntentionViewController.h"
#import "SelcetCityViewController.h"
#import "PMineResumeTradeViewController.h"
#import "PMineResumeIntentionInfoModel.h"

#import "LYCareerObjectiveSelectViewController.h"
#import "SelectJobCategoryViewController.h"

@interface PMineResumeIntentionViewController ()<PSSingleRowViewDelegate, SelcetCityViewControllerDelegate, PMineResumeTradeViewControllerDelegate, SelectJobCategoryViewControllerDelegate>
//PSSalaryRangeViewDelegate
{
    PMineResumeIntentionInfoModel *infoModel;
    
    PSCell *jobsNatureCell;
    PSCell *tradeCell;
    PSCell *catgoryCell;
    PSCell *wageCell;
    PSCell *cityCell;
    
    NSString *jobsNature;
    NSString *trade;
    NSString *catgory;
    NSString *wage;
    NSString *city;
}
@property (nonatomic, strong) PSSingleRowView *jobsNaturePickerView;
@property (nonatomic, strong) PSSingleRowView *wagePickerView;
//@property (nonatomic, strong) PSSalaryRangeView *wagePickerView;
@property (nonatomic, strong) PSSingleRowView *jobCatsPickerView;
@end

@implementation PMineResumeIntentionViewController

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.jobsNaturePickerView) {
        self.jobsNaturePickerView.hidden = YES;
    }
    if (self.wagePickerView) {
        self.wagePickerView.hidden = YES;
    }
    if (self.jobCatsPickerView) {
        self.jobCatsPickerView.hidden = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"求职意向";
    
    jobsNature = @"";
    trade = @"";
    catgory = @"";
    wage = @"";
    city = @"";
    
    [self loadData];
//    [self initUI];
}
- (void)loadData {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    
    [BaseNetManager requestWithMethod:POST path:Person_GetIntentInfo_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            
            self->infoModel = [[PMineResumeIntentionInfoModel alloc] init];
            [self->infoModel setValuesForKeysWithDictionary:data];
            
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
        [self initUI];
    } failurBlock:^(NSError * _Nonnull error) {
        [self loadData];
    }];
}
- (void)initUI {
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH-50*Scale, HeightOfStatusBar+3*Scale, 50*Scale, 36*Scale) imageName:nil target:self action:@selector(okBtnTap) title:@"确定" titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:okBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    __weak __typeof(self)weakSelf = self;
    self.jobsNaturePickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.jobsNatureArray target:self];
    self.jobsNaturePickerView.hidden = YES;
    self.wagePickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.wageArray target:self];
    self.wagePickerView.hidden = YES;
    self.jobCatsPickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.jobCatsArray target:self];
    self.jobCatsPickerView.hidden = YES;
    
    jobsNatureCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, 15*Scale, WIDTH-60*Scale, PSCellHeight) title:@"工作性质" text:((infoModel && infoModel.nature_cn.length)?infoModel.nature_cn:nil) placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    jobsNatureCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.jobsNaturePickerView.hidden = NO;
//        [strongSelf jumpToSelectPosition];
    };
    [self.view addSubview:jobsNatureCell];
    if (infoModel && infoModel.nature.length) {
        jobsNature = infoModel.nature;
    }
    
    NSString *trade1 = @"";
    if (infoModel && infoModel.trade_cn.length) {
        trade1 = infoModel.trade_cn;
        trade = infoModel.trade;
    }
    tradeCell = [[PSCell alloc] initWithFrame:CGRectMake(StartX(jobsNatureCell), EndY(jobsNatureCell), ViewWidth(jobsNatureCell), PSCellHeight) title:@"期望行业" text:trade1 placeholder:@"最多选三个" cellType:PSCellTypeOfIndicator];
    tradeCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        PMineResumeTradeViewController *PMRTVC = [[PMineResumeTradeViewController alloc] init];
        PMRTVC.delegate = strongSelf;
        [strongSelf.navigationController pushViewController:PMRTVC animated:YES];
    };
    [self.view addSubview:tradeCell];
    
    NSString *catgory1 = @"";
    if (infoModel && infoModel.catgory_cn.length) {
        catgory1 = infoModel.catgory_cn;
        catgory = infoModel.catgory_id;
    }
    catgoryCell = [[PSCell alloc] initWithFrame:CGRectMake(StartX(jobsNatureCell), EndY(tradeCell), ViewWidth(jobsNatureCell), PSCellHeight) title:@"期望职位" text:catgory1 placeholder:@"最多选三个" cellType:PSCellTypeOfIndicator];
    catgoryCell.tap = ^{
//        __strong __typeof(weakSelf)strongSelf = weakSelf;
//        strongSelf.jobCatsPickerView.hidden = NO;
        [weakSelf jumpToSelectPosition];
    };
    [self.view addSubview:catgoryCell];
    
    if (infoModel && infoModel.wage.length) {
        wage = infoModel.wage;
    }
    wageCell = [[PSCell alloc] initWithFrame:CGRectMake(StartX(jobsNatureCell), EndY(catgoryCell), ViewWidth(jobsNatureCell), PSCellHeight) title:@"期望薪资" text:((infoModel && infoModel.wage_cn.length)?infoModel.wage_cn:@"") placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    wageCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.wagePickerView.hidden = NO;
    };
    [self.view addSubview:wageCell];
    
    NSString *city1 = @"";
    if (infoModel && infoModel.district_cn.length) {
        city1 = infoModel.district_cn;
        city = infoModel.district;
    }
    cityCell = [[PSCell alloc] initWithFrame:CGRectMake(StartX(wageCell), EndY(wageCell), ViewWidth(wageCell), PSCellHeight) title:@"工作城市" text:city1 placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    cityCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        SelcetCityViewController *SCVC = [[SelcetCityViewController alloc] init];
        SCVC.delegate = strongSelf;
        [strongSelf.navigationController pushViewController:SCVC animated:YES];
    };
    [self.view addSubview:cityCell];
}
- (void)okBtnTap {
    if (jobsNatureCell.textField.text.length) {
        if (tradeCell.textField.text.length) {
            if (catgoryCell.textField.text.length) {
                if (wageCell.textField.text.length) {
                    if (cityCell.textField.text.length) {
                        [self upDate];
                    } else {
                        SVPHUDError(@"请选择期望城市");
                    }
                } else {
                    SVPHUDError(@"请选择期望薪资");
                }
            } else {
                SVPHUDError(@"请选择期望职位");
            }
        } else {
            SVPHUDError(@"请选择期望行业");
        }
    } else {
        SVPHUDError(@"请选择工作性质");
    }
}
- (void)upDate {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:wage forKey:@"wage"];
    [dic setObject:jobsNature forKey:@"job_nature"];
    [dic setObject:catgory forKey:@"job_cats"];
    [dic setObject:city forKey:@"district"];
    [dic setObject:trade forKey:@"trade"];
    
    [BaseNetManager requestWithMethod:POST path:Person_SaveIntentInfo_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(responseObject[@"msg"]);
            
            [[NSNotificationCenter defaultCenter] postNotificationName:NotificationFor_JobIntention_Change object:nil];
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(pMineResumeIntentionViewControllerBackRefresh:)]) {
                [self.delegate pMineResumeIntentionViewControllerBackRefresh:YES];
            }
            
            [self performSelector:@selector(delayClick) withObject:nil afterDelay:0.5];
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        [self loadData];
    }];
}
- (void)delayClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)psSingleRowViewCancel:(PSSingleRowView *)psSingleRowView {
    psSingleRowView.hidden = YES;
}
- (void)psSingleRowViewOk:(PublicCategoryElementModel *)model PSSingleRowView:(PSSingleRowView *)psSingleRowView {
    psSingleRowView.hidden = YES;
    if (psSingleRowView == self.jobsNaturePickerView) {
        jobsNatureCell.textField.text = model.name;
        jobsNature = model.id;
    } else if (psSingleRowView == self.wagePickerView) {
        wageCell.textField.text = model.name;
        wage = model.id;
    } else if (psSingleRowView == self.jobCatsPickerView) {
        catgoryCell.textField.text = model.name;
        catgory = model.id;
    }
}
//- (void)psSalaryRangeViewCancel:(PSSalaryRangeView *)psSalaryRangeView {
//    psSalaryRangeView.hidden = YES;
//}
//- (void)psSalaryRangeViewOkLeftData:(PublicCategoryElementModel *)leftModel rightData:(PublicCategoryElementModel *)rightModel PSSalaryRangeView:(PSSalaryRangeView *)psSalaryRangeView {
//    wageCell.textField.text = [NSString stringWithFormat:@"%@~%@", leftModel.name, rightModel.name];
//}
- (void)selcetCityViewControllerInfo:(NSDictionary *)dic {
//    cityCell.textField.text = [NSString stringWithFormat:@"%@-%@-%@", dic[@"province"], dic[@"city"], dic[@"area"]];
//    city = dic[@"cityid"];
    
    if ([[NSString stringWithFormat:@"%@", dic[@"cityid"]] integerValue] == 0) {
        cityCell.textField.text = [NSString stringWithFormat:@"%@", dic[@"province"]];
        city = [NSString stringWithFormat:@"%@", dic[@"provinceid"]];
    } else {
        cityCell.textField.text = [NSString stringWithFormat:@"%@-%@", dic[@"province"], dic[@"city"]];
        city = [NSString stringWithFormat:@"%@", dic[@"cityid"]];
    }
    
//    city = [NSString stringWithFormat:@"%@,%@,%@", dic[@"provinceid"], dic[@"cityid"], dic[@"areaid"]];
//    [NSString stringWithFormat:@"%@%@-%@%@-%@%@", dic[@"province"], dic[@"provinceid"], dic[@"city"], dic[@"cityid"], dic[@"area"], dic[@"areaid"]]
}
- (void)PMineResumeTradeViewControllerBackInfo:(NSArray *)info {
    NSLog(@"%@", info);
    NSString *name = @"";
    trade = @"";
    for (PublicCategoryElementModel *model in info) {
        trade = [NSString stringWithFormat:@"%@,%@", model.id, trade];
        name = [NSString stringWithFormat:@"%@,%@", model.name, name];
    }
    if (trade && trade.length>1) {
        trade = [trade substringToIndex:trade.length-1];
    }
    if (name && name.length>1) {
        name = [name substringToIndex:name.length-1];
    }
    tradeCell.textField.text = name;
}

#pragma mark-- Action
- (void)jumpToSelectPosition {
//    LYCareerObjectiveSelectViewController *careerVC = [[LYCareerObjectiveSelectViewController alloc] init];
//    [self.navigationController pushViewController:careerVC animated:YES];
    
    SelectJobCategoryViewController *SJCVC = [[SelectJobCategoryViewController alloc] init];
    SJCVC.navigationItem.title = @"期望职位";
    SJCVC.delegate = self;
    [self.navigationController pushViewController:SJCVC animated:YES];
}
- (void)selectJobCategoryViewControllerInfo:(SelectJobCategoryModel *)model {
    catgoryCell.textField.text = model.threeLevelName;
    catgory = model.threeLevelID;
}

@end
